/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idvalue;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "IdValue", urlPatterns = {"/IdValue"}, initParams = {
    @WebInitParam(name = "id", value = "$non"),
    @WebInitParam(name = "value", value = "$non")})
public class IdValue extends HttpServlet {

    private static ConcurrentHashMap<String, String> idValueMap = new ConcurrentHashMap<String, String>();
    private HashMap<String, Command> commandMap = new HashMap<String, Command>();

    public IdValue() {
        commandMap.put("insert", new Command("insert") {

            @Override
            public String exec(ConcurrentMap<String, String> concurrentMap, String id, String value, PrintWriter out) {
                if (idValueMap.containsKey(id)) {
                    result = "fail: var olani ekliyorsunuz";
                } else {
                    idValueMap.put(id, value);
                    result = "success: eklendi";
                }
                
                return result;
            }
        });

        commandMap.put("update", new Command("update") {

            @Override
            public String exec(ConcurrentMap<String, String> concurrentMap, String id, String value, PrintWriter out) {
                if (!idValueMap.containsKey(id)) {
                    result = "fail: olmayani guncelliyorsunuz";
                } else {
                    idValueMap.put(id, value);
                    result = "success: guncellendi";
                } return result;
            }
        });

        commandMap.put("delete", new Command("delete") {

            @Override
            public String exec(ConcurrentMap<String, String> concurrentMap, String id, String value, PrintWriter out) {
              
                if (!idValueMap.containsKey(id)) {
                    result = "fail: olmayani siliyorsunuz";
                } else {
                    idValueMap.remove(id);
                    result = "success: silindi";
                } return result;
            }
        });

        commandMap.put("list", new Command("list") {

            @Override
            public String exec(ConcurrentMap<String, String> concurrentMap, String id, String value, PrintWriter out) {
                 result = "list -----\r\n";
                for (Map.Entry<String, String> entr : idValueMap.entrySet()) {
                    result = result + entr.getKey() + ":" + entr.getValue() + "\r\n";

                } return result;
            }
        });
        
         commandMap.put("clear", new Command("clear") {

            @Override
            public String exec(ConcurrentMap<String, String> concurrentMap, String id, String value, PrintWriter out) {
                 idValueMap.clear();
                result = "success: silindi";
                return result;
            }
        });

    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String method = request.getParameter("method");
        String result = "";
        String id = request.getParameter("id");
        String value = request.getParameter("value");


        if (method == null) {
            return;
        }



        try {

            Command cmd = commandMap.get(method);
            
            if (  cmd != null){
                
                result = cmd.exec(idValueMap, id, value, out);

            } else {
                result = "fail: bilinmeyen metot";
            }



            out.write(result);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
